package org.common.utils.fasttest.entity.codec;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
import org.redisson.codec.JsonJacksonCodec;

/**
 * Redisson通用json解析
 *
 * @author duxiang
 * @date 2024/4/17.
 */
public class RedissonJacksonCodec extends JsonJacksonCodec {
    public static final RedissonJacksonCodec INSTANCE = new RedissonJacksonCodec();

    public RedissonJacksonCodec(ObjectMapper objectMapper) {
        super(objectMapper);
    }

    public RedissonJacksonCodec(ClassLoader classLoader, JsonJacksonCodec codec) {
        super(classLoader, codec);
    }

    public RedissonJacksonCodec(ObjectMapper mapObjectMapper, boolean copy) {
        super(mapObjectMapper, copy);
    }


    public RedissonJacksonCodec() {
        this(new ObjectMapper());
    }

    public RedissonJacksonCodec(ClassLoader classLoader) {
        super(classLoader);
    }

    @Override
    protected void init(ObjectMapper objectMapper) {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker().withFieldVisibility(JsonAutoDetect.Visibility.ANY).withGetterVisibility(JsonAutoDetect.Visibility.NONE).withSetterVisibility(JsonAutoDetect.Visibility.NONE).withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        objectMapper.addMixIn(Throwable.class, ThrowableMixIn.class);
        objectMapper.registerModule(new EclipseCollectionsModule());
    }
}
